package com.showcai.service.impl;

import com.showcai.dao.ExpressInfoMapper;
import com.showcai.module.ApplyWatch;
import com.showcai.module.ExpressInfo;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.ExpressInfoService;
import com.showcai.service.OrderService;
import com.showcai.tools.KdniaoSubscribeAPI;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-7-17.
 */
@Service
public class ExpressInfoServiceImpl implements ExpressInfoService {
    @Autowired
    ExpressInfoMapper expressInfoMapper;
@Autowired
    OrderService orderService;
@Autowired ApplyWatchServiceImpl applyWatchService;
    private void addExpressInfo(ExpressInfo expressInfo) {

        expressInfoMapper.insertExpressInfo(expressInfo);
        ApplyWatch applyWatch=(ApplyWatch)applyWatchService.selectApplyByOrderId(expressInfo.getOrderId());
        if(applyWatch==null){
            throw new MyRunTimeExcption("报名订单不存在！");
        }
        applyWatch.setStatus(OrderService.DISPATCHED);
        applyWatchService.modifyApply(applyWatch);
        orderService.modifyOrderStatus(expressInfo.getOrderId(),OrderService.DISPATCHED);

    }
    private void updateExpressInfo(ExpressInfo expressInfo) {
        expressInfoMapper.updateByPrimaryKeySelective(expressInfo);
    }

   @Transactional
    @Override
    public void modifyExpressInfo(ExpressInfo expressInfo) {
        if (expressInfo.getCreateTime() == null) {
            expressInfo.setCreateTime(new Date());
        }
         Long orderId=expressInfo.getOrderId();

        Map<String, String> map = new HashMap<>();
        map.put("orderId", String.valueOf(orderId));
        ExpressInfo expressInfo1 = expressInfoMapper.selectExpressInfo(map);
        if (expressInfo1 == null) {
            this.addExpressInfo(expressInfo);
        } else {

            String expressName = expressInfo.getExpressName();
            if (StringUtils.isNotBlank(expressName)) {
                expressInfo1.setExpressName(expressName);
            }
            String expressCode = expressInfo.getExpressCode();
            if (StringUtils.isNotBlank(expressCode)) {
                expressInfo1.setExpressCode(expressCode);
            }
            String expressOrderId = expressInfo.getExpressOrderId();
            if (StringUtils.isNotBlank(expressOrderId)) {
                expressInfo1.setExpressOrderId(expressOrderId);
            }
            String info = expressInfo.getInfo();
            if (StringUtils.isNotBlank(info)) {
                expressInfo1.setInfo(info);
            }

            updateExpressInfo(expressInfo1);
        }

    }

    @Override
    public ExpressInfo selectExpressInfo(Map<String, String> map) {
        return expressInfoMapper.selectExpressInfo(map);
    }


    @Override
    public String checkLogistics(Long orderItemId) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("orderItemId", String.valueOf(orderItemId));
        ExpressInfo expressInfo = expressInfoMapper.selectExpressInfo(map);
        if (expressInfo == null) {
            throw new MyRunTimeExcption("还没有发货！");
        }
        String expCode = expressInfo.getExpressCode();
        String expNo = expressInfo.getExpressOrderId();
        return KdniaoSubscribeAPI.getOrderTracesByJson(expCode, expNo);
    }

    @Override
    public List<ExpressInfo> selectExpressInfosTask() {
        return expressInfoMapper.selectExpressInfosTask();
    }

    @Override
    public ExpressInfo selectExpressInfoByOrderId(Long orderId) {
        return expressInfoMapper.selectExpressInfoByOrderId(orderId);
    }
}
